﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;

namespace WPFExcelReport
{
    public partial class TWBBS13
    {
        #region ==AUF===========
        public static readonly DependencyProperty AUFProperty = DependencyProperty.Register("AUF",
            typeof(string), typeof(TWBBS13),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFChanged));
        public string AUF
        {
            get
            {
                return (string)GetValue(AUFProperty);
            }
            set
            {
                SetValue(AUFProperty, value);
                ORDERNUM = value;
            }
        }
        private static void OnAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWBBS13;
            if (src != null)
            {
                src.AUF = (string)e.NewValue; 
            }
        }
        #endregion
        #region ==IsProduced===========
        public static readonly DependencyProperty IsProducedProperty = DependencyProperty.Register("IsProduced",
            typeof(bool), typeof(TWBBS13),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsProducedChanged));
        public bool IsProduced
        {
            get
            {
                return (bool)GetValue(IsProducedProperty);
            }
            set
            {
                SetValue(IsProducedProperty, value);
            }
        }
        private static void OnIsProducedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWBBS13;
            if (src != null)
            {
                src.IsProduced = (bool)e.NewValue;
            }
        }
        #endregion 
        #region ==BS===========
        public static readonly DependencyProperty BSProperty = DependencyProperty.Register("BS",
            typeof(string), typeof(TWBBS13),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBSChanged));
        public string BS
        {
            get
            {
                return (string)GetValue(BSProperty);
            }
            set
            {
                SetValue(BSProperty, value);
            }
        }
        private static void OnBSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWBBS13;
            if (src != null)
            {
                src.BS = (string)e.NewValue;
            }
        }
        #endregion
        public TWBBS13()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBEW();
            this.DataContext = MV;
            InitializeComponent();
            BS = "13";
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
         }
        string LastAUF = "";
        public void Load(string value)
        {
            switch (app.company)
            {
                default:
                case "JLT":
                case "YGR":
                    LoadJLT(value);
                    break;
            }
        }
        public override void LOADBYAUF()
        {
            Load(ORDERNUM);
            base.LOADBYAUF();
        }
        public void LoadJLT(string value)
        {

            if (value.Length < 2)
                return;
            IsProduced = false;
            DataRow dwData = TBMain.NewRow();
            string ordertype = Helpers.Helper.Left(value, 1);

            dwData["WBEWMANDANT"] = app.Mandant;
            dwData["WBEWAUFKRS"] = value.Substring(1, 2);
            dwData["WBEWAUFLFD"] = value.Substring(3, 6);
            dwData["WBEWAUFPOS"] = value.Substring(9, 3);
            dwData["WBEWBS"] = BS;
            DataSet SResult = ((VWBBEW)MV).QueryWBBEW(dwData, 7);
            MergeData(SResult, false, true);

            int idx = 0;
            string AGANG = "";

            MessageInfo msgs = new MessageInfo();
            string msg = "";
            msgs.MSGTYPE = "WBBS13";
            msgs.Sender = this.RUNID;
            msgs.ACTION = "WBBS13";
            msgs.AUFTYP = "V";

            foreach (DataRow row in TBMain.Rows)
            {
                idx++;
                msgs.KRS = row["ABPAUFKRS"].ToString();
                msgs.LFD = row["ABPAUFLFD"].ToString();
                msgs.POS = row["ABPAUFPOS"].ToString();
                idx++;
                msgs.P1 = row["QTY"].ToString();
                msgs.P2 = Helper.Right(row["MDATE"].ToString(), 2);
                if (row["ABPAUFART"].ToString() == "VA")
                {
                    msgs.P3 = "6-后整理";
                }
                if (row["ABPAUFART"].ToString() == "PR")
                {
                    msgs.P3 = "5-织造";
                }
                VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", dwMaster, msg, msgs);
            }
            if (idx > 0)
            {
                IsProduced = true;
            }
        } 
    }
}
